package org.homeworkadmin.admin.controller;


import org.apache.ibatis.annotations.Delete;
import org.homeworkadmin.admin.domain.Admins;
import org.homeworkadmin.admin.domain.Users;
import org.homeworkadmin.admin.service.AdminsService;
import org.homeworkadmin.common.core.controller.BaseController;
import org.homeworkadmin.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admins")
public class AdminsController extends BaseController {

    @Autowired
    private AdminsService adminsService;

    @GetMapping("/get")
    public AjaxResult getUsers(Integer adminId,String adminAccount,String adminPassword,String adminName,String adminIdcard,String adminPhone,int pagesize,int currpage) {
        System.out.println("pagesize" + pagesize);
        System.out.println("currpage" + currpage);
        currpage = (currpage - 1) * pagesize;
        int total = adminsService.getCount(adminAccount,adminName,adminPhone);
        List<Admins> list = adminsService.get(adminId,adminAccount,adminPassword,adminName,adminIdcard,adminPhone,pagesize,currpage);
        System.out.println(list + "   " + total);
        return new AjaxResult(200, "success", list,total);
    }


    @PostMapping("/insert")
    public AjaxResult addAdmins(@RequestBody Admins admins){
        return success(adminsService.insert(admins));
    }
    @PostMapping("/update")
    public AjaxResult updateAdmins(@RequestBody Admins admins){
        return success(adminsService.update(admins));
    }

    @DeleteMapping("/delete/{adminId}")
    public ResponseEntity<Void> deleteAdmin(@PathVariable(value = "adminId") Long adminId) {
        try {
            adminsService.delete(adminId);
            return ResponseEntity.noContent().build();
        } catch (Exception e) {
            logger.error("Failed to delete admin with ID: {}", adminId, e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }


}
